// pages/authlogin/index.js
const config = require("../../utils/config");
const utils = require("../../utils/util");
import {http} from "../../utils/request";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userHead: config.hostUrl + "/upload/apiimg/ic-default-head.png", //默认图片
    userName: null,
    userPhone: null,
    isCheck: false,
    from: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      from: options.from?options.from:""
    })
    // console.log("授权登录来自哪里：", this.data.from)
  },

  /** 更改头像 */
  onChooseAvatar(e) {
    let that = this;
    wx.uploadFile({
      url: config.apiHost + "File/UploadFile?fileModule=1",
      filePath: e.detail.avatarUrl,   
      name: 'file',
      success(res){
        if(res.data){
          let e = JSON.parse(res.data)
          if(e.Tag == 10){
            that.setData({
              userHead: config.hostUrl + e.Data,
            })
          }
        }
      },
      fail(err){
        http.showToast("头像上传失败");
      }
    })
  },

   /** 昵称改变事件 */
  changeNameTap(e){
    this.setData({
      userName: e.detail.value,
    })
  },
  /** 昵称输入事件 */
  inputNameTap(e){
    this.setData({
      userName: e.detail.value,
    })
  },

  /** 手机号输入 */
  inputPhoneTap(e){
    this.setData({
      userPhone: e.detail.value
    })
  },

  /** 登录一键登录 */
  getPhoneNumber(e){
    let that = this;
    if(e.detail.errMsg == "getPhoneNumber:ok"){
      http.requestData("POST","WeChat/GetPhoneNumber",{
        code: e.detail.code,
        appletId: config.OPENID()
      }).then(res=>{
        if(res.Tag == 10){
          that.setData({
            userPhone: res.Message
          })
          that.saveUserInfo();
        }else{
          http.showToast(res.Message);
        }
      })
    }else{
      http.showToast("您已拒绝获取手机号");
    }
  },

  /** 登录 */
  loginTap(){
    // if(!this.data.userHead){
    //   return http.showToast("请上传头像");
    // }
    if(!this.data.userName){
      return http.showToast("请输入昵称");
    }
    if(!this.data.userPhone){
      return http.showToast("请输入手机号");
    }
    if(!utils.checkPhone(this.data.userPhone)){
      return http.showToast("手机号格式错误");
    }
    if(!this.data.isCheck){
      return http.showToast("请先阅读并同意用户协议");
    }
    this.saveUserInfo();
  },

   /** 保存用户信息 */
   saveUserInfo(){
    let that = this;
    http.requestData("POST","WeChat/Save",{
      AppletId: config.OPENID(),
      portrait: this.data.userHead,
      nickname: this.data.userName,
      mobile: this.data.userPhone,
      gender: "",
      birthday: ""
    },true).then(res=>{
      if(res.Tag == 10){
        let userInfo = wx.getStorageSync(config.LOGIN_KEY);
        userInfo.UID = res.Data;
        userInfo.TEL = that.data.userPhone;
        userInfo.ROLE = config.ROLE()?config.ROLE():false;
        userInfo.NICKNAME = that.data.userName;
        userInfo.HEADURL = that.data.userHead;
        wx.setStorageSync(config.LOGIN_KEY, userInfo);
        config.getLoginInfo();
        if(that.data.from == "me"){
          wx.switchTab({
            url: '/pages/homepage/index',
          })
        }else{
          wx.navigateBack();
        }
      }else{
        http.showToast(res.Message);
      }
    }).catch(err=>{
      http.showToast("服务器繁忙，请稍后重试")
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /** change checkbox */
  changeCheckTap(){
    this.setData({
      isCheck: !this.data.isCheck
    })
  },

  /** 用户协议 */
  agreenmentTap(){
    http.href("/pages/authlogin/userProtocol/index");
    // wx.navigateTo({
    //   url: '/pages/authlogin/agreenment/index?type=1',
    // })
  },
  /** 隐私政策 */
  privacyTap(){
    http.href("/pages/authlogin/privacyPolicy/index");
    // wx.navigateTo({
    //   url: '/pages/authlogin/agreenment/index?type=2',
    // })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: "创时代助手",
      path: "/pages/authlogin/index?from=me",
      success(res){
        console.log("分享成功==", res)
      },
      fail(err){
        console.log("分享失败==", err)
      }
    }
  }
})